package VWorldGameStudio;

import FCRendering.FCRBattleOrderConstant;
import FCRendering.FCRConstants;
import FCRendering.FCRMaster;
import FCRendering.ImageUtil;
import lombok.Getter;
import lombok.Setter;
import socket.FCRSocketClient;
import socket.FCRSocketServer;
import utils.ClickHelper;
import Function.FCItemForTankHelper;
import utils.FCMixUtil;

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;

import javax.imageio.ImageIO;

import static FCRendering.FCRMaster.*;

/**
 * 坦克属性类
 */
public class FCTank extends FCPropulsion implements Cloneable {
    public FCTank() {
        hostId = FCRMaster.gameSave.HOST_UID;
    }

    private ClickHelper clickHelper = new ClickHelper(2000);

    //坦克像人一样也有hostId
    @Getter
    @Setter
    private String hostId;
    // 坦克ID
    @Setter
    @Getter
    private String ID;

    @Setter
    @Getter
    private String type = "owner";//如果是 “Taxi"就是出租车，出租车不能被改造，必须是买下来才能
    // 坦克名字
    @Setter
    @Getter
    private String name = "";
    // 主炮id
    @Setter
    @Getter
    private String gunID;
    // 副炮id
    @Setter
    @Getter
    private String gun2ID;
    //SEID
    @Getter
    @Setter
    private String SEID;

    // 发动机id
    @Setter
    @Getter
    private String engineID;
    // C装置id
    @Setter
    @Getter
    private String CdeviceID;
    // 底盘id
    @Setter
    @Getter
    private String chassisID;
    // 雷达id
    @Setter
    @Getter
    private String radarID;
    // 图片路径
    @Setter
    @Getter
    private String imageMasterUrl;
    @Setter
    @Getter
    private String imageWhiteLineUrl;
    @Setter
    @Getter
    private String imageDirUrl;
    // 主身份图片

    @Setter
    @Getter
    private int[] imageMasterSize = new int[]{32, 32};
    // 白色纹路图片

    @Getter
    @Setter
    //TODO 残留物，鸟粪或者锈铁要清洗才能去掉
    private ArrayList<String> residues = new ArrayList<>();

    @Getter
    @Setter//是否中酸了，中酸后每回合战斗或者行走2步将减少5%甲
    private boolean isAcidState = false;

    @Getter
    @Setter
    //当前甲片值，0.01t 一片 100防就是1t，如果总体重超了不能移动
    private int defense = 0;

    @Getter
    @Setter
    //能放的最大的甲片值是多少，公式是：底盘承受t + 发动机马力t - 总重量
    private int maxDefense = 0;

    @Getter
    @Setter
    private double allWeight = 0;//总重量 公式是：主炮重量 + 副炮重量 + 底盘重量 + 发动机重量 + 背包重量

    @Getter
    @Setter
    //能承受多少t 公式是：底盘承受t + 发动机马力t
    private double enableCarryWeight = 0;

    @Getter
    @Setter
    //剩余空闲多少重量,判断这个值，大于0就可以走，小于0就是不能走了，超重了，提示
    private double leftWeight = 0;

    @Getter
    @Setter
    private double residuesWeight = 0;//鸟粪重量

    public boolean canMove() {
        if (leftWeight < 0) {
            if (clickHelper.canClick()) {
                System.out.println("超重 " + leftWeight + "t");
                fcv.getMessage().add(name + " 超重 " + leftWeight + "t");
                fcv.showMessage();
            }
            return false;
        }
        FCItem item = FCRMaster.itemLib.getItemOfID(engineID);
        if (item == null) {
            if (clickHelper.canClick()) {
                System.out.println("没有发动机");
                fcv.getMessage().add(name + " 没有发动机");
                fcv.showMessage();
            }
            return false;
        }
        if (item.getCurrentDefense() <= 0) {
            if (clickHelper.canClick()) {
                System.out.println("发动机损坏了");
                fcv.getMessage().add(name + " 发动机损坏了");
                fcv.showMessage();
            }
            return false;
        }
        item = FCRMaster.itemLib.getItemOfID(chassisID);
        if (item == null) {
            if (clickHelper.canClick()) {
                System.out.println("没有底盘");
                fcv.getMessage().add(name + " 没有底盘");
                fcv.showMessage();
            }
            return false;
        }
        if (item.getCurrentDefense() <= 0) {
            if (clickHelper.canClick()) {
                System.out.println("底盘损坏了");
                fcv.getMessage().add(name + " 底盘损坏了");
                fcv.showMessage();
            }
            return false;
        }
        item = FCRMaster.itemLib.getItemOfID(CdeviceID);
        if (item == null) {
            if (clickHelper.canClick()) {
                System.out.println("没有C装置");
                fcv.getMessage().add(name + " 没有C装置");
                fcv.showMessage();
            }
            return false;
        }
        if (item.getCurrentDefense() <= 0) {
            if (clickHelper.canClick()) {
                System.out.println("C装置损坏了");
                fcv.getMessage().add(name + " C装置损坏了");
                fcv.showMessage();
            }
            return false;
        }
        return true;
    }


    //每次装备坦克和背包添加减少，战斗前等就要调一次，初始化上面属性
    public void reInitShuxing() {
        allWeight = 0;
        enableCarryWeight = 0;
        residuesWeight = 0;
        FCItem item = null;
        item = FCRMaster.itemLib.getItemOfID(chassisID);
        //底盘
        if (item != null) {
            allWeight += (item.getWeight() + FCItemForTankHelper.getAllShellWeight(item));
            //底盘改造增加的守备力重量
            allWeight += item.getDefenseUpgradeAdd() * FCRConstants.DEFENSE_UPGRADE_weight;

            enableCarryWeight += item.getCapacity();
            if (item.getCannonHoles()[0] == 0) {
                if (gunID != null && !gunID.equals("null")) {
                    if (type.equals("owner")) {
                        //TODO 注意这个洞已经堵住了的，所有要把枪放到背包，要么就丢弃
                        if (!addItemToKnapsack(gunID)) {
                            fcv.getMessage().add("因底盘主炮洞被堵住，坦克背包空间不足，丢弃主炮-" + item.getChName());
                            fcv.showMessage();
                        } else {
                            fcv.getMessage().add("底盘主炮穴没开,卸下主炮!");
                            fcv.showMessage();
                        }
                        gunID = "null";
                    } else {
                        item.getCannonHoles()[0] = 1;
                        allWeight += FCRConstants.GUN1_hole_weight;
                    }
                }
            } else {
                //TODO 一个主洞增重
                allWeight += FCRConstants.GUN1_hole_weight;
            }
            if (item.getCannonHoles()[1] == 0) {
                if (gun2ID != null && !gun2ID.equals("null")) {
                    if (type.equals("owner")) {
                        //TODO 注意这个洞已经堵住了的，所有要把枪放到背包，要么就丢弃
                        if (!addItemToKnapsack(gun2ID)) {
                            fcv.getMessage().add("因底盘副炮洞被堵住，坦克背包空间不足，丢弃副炮-" + item.getChName());
                            fcv.showMessage();
                        } else {
                            fcv.getMessage().add("因盘副炮穴没开,卸下副炮!");
                            fcv.showMessage();
                        }
                        gun2ID = "null";
                    } else {
                        item.getCannonHoles()[1] = 1;
                        allWeight += FCRConstants.GUN2_hole_weight;
                    }
                }
            } else {
                //TODO 一个副洞增重
                allWeight += FCRConstants.GUN2_hole_weight;
            }
            if (item.getCannonHoles()[2] == 0) {
                if (SEID != null && !SEID.equals("null")) {
                    if (type.equals("owner")) {
                        //TODO 注意这个洞已经堵住了的，所有要把枪放到背包，要么就丢弃
                        if (!addItemToKnapsack(SEID)) {
                            fcv.getMessage().add("因底盘SE穴被堵住，坦克背包空间不足，丢弃SE-" + item.getChName());
                            fcv.showMessage();
                        } else {
                            fcv.getMessage().add("因底盘SE穴没开,卸下SE!");
                            fcv.showMessage();
                        }
                        SEID = "null";
                    } else {
                        item.getCannonHoles()[2] = 1;
                        allWeight += FCRConstants.SE_hole_weight;
                    }
                }
            } else {
                //TODO 一个SE洞增重
                allWeight += FCRConstants.SE_hole_weight;
            }

            //TODO 默认一个弹仓是0.22t
            allWeight += (item.getShellCaseNum() * FCRConstants.SHELL_hole_weight);
        }

        item = FCRMaster.itemLib.getItemOfID(gunID);
        if (item != null) {
            allWeight += item.getWeight();
        }
        item = FCRMaster.itemLib.getItemOfID(gun2ID);
        if (item != null) {
            allWeight += item.getWeight();
        }
        item = FCRMaster.itemLib.getItemOfID(SEID);
        if (item != null) {
            allWeight += item.getWeight();
        }
        item = FCRMaster.itemLib.getItemOfID(engineID);
        if (item != null) {
            allWeight += item.getWeight();
            enableCarryWeight += item.getCapacity();
        }
        item = FCRMaster.itemLib.getItemOfID(radarID);
        if (item != null) {
            allWeight += item.getWeight();
        }
        item = FCRMaster.itemLib.getItemOfID(CdeviceID);
        if (item != null) {
            allWeight += item.getWeight();
        }
        for (String k : knapsack) {
            item = FCRMaster.itemLib.getItemOfID(k);
            if (item != null) {
                allWeight += item.getWeight();
            }
        }
        for (String itemName : residues) {
            FCItem item1 = itemLib.getItemOfID(itemName);
            residuesWeight += item1.getWeight();
        }
        allWeight += ((double) defense / 100);
        residuesWeight = Double.parseDouble(String.format("%.2f", residuesWeight));
        allWeight += residuesWeight;
        enableCarryWeight = Double.parseDouble(String.format("%.2f", enableCarryWeight));
        allWeight = Double.parseDouble(String.format("%.2f", allWeight));
        leftWeight = Double.parseDouble(String.format("%.2f", enableCarryWeight - allWeight));
        maxDefense = (int) (leftWeight * 100 + defense);//1t 100 防
        maxDefense = maxDefense < 0 ? 0 : maxDefense;
        System.out.println("坦克 总重量 " + allWeight + " 承受： " + enableCarryWeight + " sp:" + defense);
    }


    /**
     * 坦克背包，总容量10
     */
    @Getter
    @Setter
    private String[] knapsack = new String[10];

    public String getKnapsackItemIDOfNum(int num) {
        return this.knapsack[num];
    }

    public boolean itemMoveOfCase(int num, String itemid) {
        if (this.knapsack[num] == null || this.knapsack[num].contains("null")) {
            if (itemid != null && !itemid.contains(",")) {
                FCItem item = itemLib.getItemOfID(itemid);
                if (item != null && item.getCatalog().equals("tank")) {
                    itemid = gameSave.checkTankItem(itemid);//为什么要用这个函数呢，因为所有放到坦克背包里的坦克道具（人类道具不会）都要备份一个对象，
                }
            }
            this.knapsack[num] = itemid;
            return true;
        } else
            return false;
    }

    public void insertItemToKnapsack(int num, String itemid) {
        if (itemid != null && !itemid.contains(",")) {
            FCItem item = itemLib.getItemOfID(itemid);
            if (item != null && item.getCatalog().equals("tank")) {
                itemid = gameSave.checkTankItem(itemid);//为什么要用这个函数呢，因为所有放到坦克背包里的坦克道具（人类道具不会）都要备份一个对象，
            }
        }
        this.knapsack[num] = itemid;
    }

    /**
     * 给背包添加物品
     *
     * @param itemid
     * @return
     */
    public boolean addItemToKnapsack(String itemid) {
        for (int i = 0; i < knapsack.length; i++)
            if (knapsack[i] == null || knapsack[i].length() < 1 || knapsack[i].equals("null")) {
                if (itemid != null && !itemid.contains(",")) {
                    FCItem item = itemLib.getItemOfID(itemid);
                    if (item != null && item.getCatalog().equals("tank")) {
                        itemid = gameSave.checkTankItem(itemid);//为什么要用这个函数呢，因为所有放到坦克背包里的坦克道具（人类道具不会）都要备份一个对象，
                    }
                }
                knapsack[i] = itemid;
                return true;
            }
        return false;
    }

    public boolean hasEmptyBag() {
        for (int i = 0; i < knapsack.length; i++) {
            if (knapsack[i] == null || knapsack[i].equals("null")) {
                return true;
            }
        }
        return false;
    }

    /**
     * 注册
     *
     * @param name
     * @param gunID
     * @param gun2ID
     * @param engineID
     * @param CdeviceID
     * @param chassisID
     * @param radarID
     * @param masterPngUrl
     * @param actionPngUrl
     * @param whiteLinePngUrl
     */
    public void registerTank(String name, String gunID, String gun2ID, String SEID, String engineID, String CdeviceID,
                             String chassisID, String radarID, String masterPngUrl, String actionPngUrl, String whiteLinePngUrl) {
        this.CdeviceID = CdeviceID;
        this.chassisID = chassisID;
        this.engineID = engineID;
        this.gun2ID = gun2ID;
        this.gunID = gunID;
        this.ID = name;
        this.name = name;
        this.SEID = SEID;
        this.radarID = radarID;
        this.imageMasterUrl = masterPngUrl;
        this.imageWhiteLineUrl = whiteLinePngUrl;
        this.imageDirUrl = actionPngUrl;
        try {
            BufferedImage a = ImageIO.read(new File(masterPngUrl));
            imageMasterSize = new int[]{a.getWidth(), a.getHeight()};
        } catch (Exception e) {
            // TODO: handle exception
        }
        initDirOfPng(actionPngUrl);
    }

    /**
     * 初始化方向图片
     *
     * @param url
     */
    private void initDirOfPng(String url) {
        try {
            if (!ImageUtil.getImageUtil().getTankImgWrapHashMap().containsKey(url)) {
                ImageUtil.TankImgWrap tankImgWrap = new ImageUtil.TankImgWrap();
                BufferedImage im = ImageIO.read(new File(url));
                int perW = im.getWidth() / 4;
                int perH = im.getHeight() / 4;
                for (int i = 0; i < 4; i++)
                    tankImgWrap.imageDown[i] = im.getSubimage(perW * i, 0, perW, perH);
                for (int i = 0; i < 4; i++)
                    tankImgWrap.imageLeft[i] = im.getSubimage(perW * i, perH, perW, perH);
                for (int i = 0; i < 4; i++)
                    tankImgWrap.imageRight[i] = im.getSubimage(perW * i, perH * 2, perW, perH);
                for (int i = 0; i < 4; i++)
                    tankImgWrap.imageUp[i] = im.getSubimage(perW * i, perH * 3, perW, perH);

                ImageUtil.getImageUtil().getTankImgWrapHashMap().put(url, tankImgWrap);
            }
        } catch (Exception e) {
            System.out.println("坦克行走图报错 " + url);
            e.printStackTrace();
        }
    }

    /**
     * 获取图片
     *
     * @param dir
     * @param num
     * @return
     */
    public Image getImageOfDirAndActiveNum(int dir, int num) {
        return ImageUtil.getImageUtil().getTankMoveImg(imageDirUrl, dir, num);
    }

    public Image getImageWhiteLine() {
        return Toolkit.getDefaultToolkit().getImage(imageWhiteLineUrl);
    }

    public Image getImageMaster() {
        return Toolkit.getDefaultToolkit().getImage(imageMasterUrl);
    }


    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("ID:" + ID);
        stringBuilder.append(" name:" + name);
        stringBuilder.append(" gunID:" + gunID);
        stringBuilder.append(" gun2ID:" + gun2ID);
        stringBuilder.append(" SEID:" + SEID);
        stringBuilder.append(" engineID:" + engineID);
        stringBuilder.append(" CdeviceID:" + CdeviceID);
        stringBuilder.append(" chassisID:" + chassisID);

        return stringBuilder.toString();
    }

    public String toString2() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(" masterUrl:" + imageMasterUrl);
        stringBuilder.append(" whiteLineUrl:" + imageWhiteLineUrl);
        stringBuilder.append(" DirUrl:" + imageDirUrl);
        return stringBuilder.toString();
    }

    /**
     * 克隆
     *
     * @return
     */
    public FCTank getClone() {
        try {
            return (FCTank) super.clone();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    @Override
    protected void interSetIsBeingTow(boolean flag) {
        FCTank fcTankSaveIos = FCRMaster.gameSave.findTankSaveIos(getID());
        if (fcTankSaveIos == null) {
            reInitShuxing();
            //备份坦克道具
            setChassisID(gameSave.checkTankItem(getChassisID()));
            setEngineID(gameSave.checkTankItem(getEngineID()));
            setRadarID(gameSave.checkTankItem(getRadarID()));
            setGunID(gameSave.checkTankItem(getGunID()));
            setGun2ID(gameSave.checkTankItem(getGun2ID()));
            setCdeviceID(gameSave.checkTankItem(getCdeviceID()));
            setSEID(gameSave.checkTankItem(getSEID()));
            for (int i = 0; i < 10; i++) {
                FCItem item = itemLib.getItemOfID(knapsack[i]);
                if (item != null && item.getCatalog().equals("tank")) {
                    knapsack[i] = gameSave.checkTankItem(knapsack[i]);
                }
            }

            //存档要
            if (!gameSave.getTankSaveIos().containsKey(getID())) {
                gameSave.getTankSaveIos().put(getID(), this);
            }
            if (fcv.isSever()) {
                FCRSocketServer.getIn().sendTanksSync();
            } else if (fcv.isClient()) {
                FCRSocketClient.getIn().sendTanksSync();
            }
            fcv.getLocalPropulsions().add(this);
        }
    }

    //作为牵引物的接口
    @Override
    public void onPropulsionChanged() {

    }

    @Override
    public boolean canTow() {
        return true;
    }

    @Override
    public Image getPropulsionImage() {
        return ImageUtil.getImageUtil().getTankMoveImg(imageDirUrl, getPdir(), getPactionNum());
    }

    @Override
    public String getPropulsionName() {
        return getName();
    }

    @Override
    public String getPropulsionType() {
        return "tank";
    }

    @Override
    public String getPropulsionHostId() {
        return getHostId();
    }

    @Override
    public String getPropulsionID() {
        return getID();
    }


    //底盘
    public String getCAText(FCItem item) {
        int holes = 0;
        for (int i : item.getCannonHoles()) {
            if (i == 1) {
                holes++;
            }
        }
        //要加上弹仓重量
        return item.getChName() + "  " + (item.getWeight() + item.getShellCaseNum() * 2) + "t"
                + "  承:" + item.getCapacity() + "t " + holes + "穴";
    }

    //发动机
    public String getEText(FCItem item) {
        return item.getChName() + "  " + item.getWeight() + "t" + "  承:" + item.getCapacity() + "t";
    }

    public String getSCText(FCItem item) {
        return item.getChName() + "  " + item.getWeight() + "t";
    }

    public String getGun1Text(FCItem item) {
        return item.getChName() + "  " + item.getWeight() + "t" + "  弹:" + (item.getCapacity() == 0 ? "∞" : (item.getCurrentCapacityVal() + "/" + (int) item.getCapacity()));
    }

    public String getSEText(FCItem item) {
        return item.getChName() + "  " + item.getWeight() + "t" + "  弹:" + (item.getCapacity() == 0 ? "∞" : (item.getCurrentCapacityVal() + "/" + (int) item.getCapacity()));
    }

    public String getGun2Text(FCItem item) {
        return item.getChName() + "  " + item.getWeight() + "t" + "  弹:∞";
    }

    //C装置 有特性的
    public String getCText(FCItem item) {
        return item.getChName() + "  " + item.getWeight() + "t" + "  特:无";
    }

    //当前sp信息
    public String getSPText() {
        return defense + "/" + maxDefense;
    }

    public String getEWText() {
        return enableCarryWeight + "t";
    }

    public String getAWText() {
        return allWeight + "t";
    }

    //战斗中，C装置是否oK
    public boolean isCdeviceOk() {
        FCItem item = FCRMaster.itemLib.getItemOfID(CdeviceID);
        if (item != null && item.getCurrentDefense() > 0) {
            return true;
        }
        return false;
    }

    public boolean isEngineOk() {
        FCItem item = FCRMaster.itemLib.getItemOfID(engineID);
        if (item != null && item.getCurrentDefense() > 0) {
            return true;
        }
        return false;
    }

    //战斗中，主炮是否oK
    public boolean isGun1Ok() {
        FCItem item = FCRMaster.itemLib.getItemOfID(gunID);
        if (item != null && item.getCurrentDefense() > 0) {
            return true;
        }
        return false;
    }

    //战斗中，副炮是否oK
    public boolean isGun2Ok() {
        FCItem item = FCRMaster.itemLib.getItemOfID(gun2ID);
        if (item != null && item.getCurrentDefense() > 0) {
            return true;
        }
        return false;
    }

    //战斗中，SE是否oK
    public boolean isSEOk() {
        FCItem item = FCRMaster.itemLib.getItemOfID(SEID);
        if (item != null && item.getCurrentDefense() > 0) {
            return true;
        }
        return false;
    }

    //战斗中，底盘是否oK
    public boolean isDiPanOk() {
        FCItem item = FCRMaster.itemLib.getItemOfID(chassisID);
        if (item != null && item.getCurrentDefense() > 0) {
            return true;
        }
        return false;
    }


    public String getDetail() {
        StringBuilder sb = new StringBuilder();
        sb.append(getName() + " --战车中国造");
        if (residues.size() > 0) {
            sb.append(" 残留鸟粪" + residues.size() + "个 重量" + residuesWeight + "t 建议洗车");
        }
        return sb.toString();
    }

    //TODO 找到最低防御的部件
    public FCItem findTheMaxLowDefenseItem() {
        ArrayList<FCItem> list = new ArrayList<>(0);
        //本地的才有效
        FCItem item = itemLib.getItemOfID(getChassisID());
        if (item != null && item.getCurrentDefense() != 0) {
            list.add(item);
        }
        item = itemLib.getItemOfID(getGunID());
        if (item != null && item.getCurrentDefense() != 0) {
            list.add(item);
        }
        item = itemLib.getItemOfID(getGun2ID());
        if (item != null && item.getCurrentDefense() != 0) {
            list.add(item);
        }
        item = itemLib.getItemOfID(getSEID());
        if (item != null && item.getCurrentDefense() != 0) {
            list.add(item);
        }
        item = itemLib.getItemOfID(getCdeviceID());
        if (item != null && item.getCurrentDefense() != 0) {
            list.add(item);
        }
        FCItem retItem = null;
        for (FCItem fcItem : list) {
            if (retItem == null) {
                retItem = fcItem;
            } else {
                if (fcItem.getCurrentDefense() < retItem.getCurrentDefense()) {
                    retItem = fcItem;
                }
            }
        }
        return retItem;
    }

    public boolean isTankAllBroken() {
        //STEP 1 先判断是不是出租车，如果是出粗车，只是损坏就会被强制回收掉
        //STEP 2 如果严重损坏的是发动机、C装置或底盘，则会立即强制下车
        if (type.equals("owner")) {
            return !isDiPanOk() && !isEngineOk() && !isCdeviceOk();
        } else {
            FCItem item = itemLib.getItemOfID(getChassisID());
            int allDefense = 0;
            if (item != null) {
                allDefense = item.getDefenseAdd() + item.getDefenseUpgradeAdd();
                if (item.getCurrentDefense() <= allDefense / 2) {
                    return true;
                }
            }
            item = itemLib.getItemOfID(getGunID());
            if (item != null) {
                allDefense = item.getDefenseAdd() + item.getDefenseUpgradeAdd();
                if (item.getCurrentDefense() <= allDefense / 2) {
                    return true;
                }
            }
            item = itemLib.getItemOfID(getGun2ID());
            if (item != null) {
                allDefense = item.getDefenseAdd() + item.getDefenseUpgradeAdd();
                if (item.getCurrentDefense() <= allDefense / 2) {
                    return true;
                }
            }
            item = itemLib.getItemOfID(getSEID());
            if (item != null) {
                allDefense = item.getDefenseAdd() + item.getDefenseUpgradeAdd();
                if (item.getCurrentDefense() <= allDefense / 2) {
                    return true;
                }
            }
            item = itemLib.getItemOfID(getCdeviceID());
            if (item != null) {
                allDefense = item.getDefenseAdd() + item.getDefenseUpgradeAdd();
                if (item.getCurrentDefense() <= allDefense / 2) {
                    return true;
                }
            }
            return false;
        }
    }

    //获取坦克的命中率
    public int getHitRate(FCPlayerBattleWrap wrap, int driverSpeed, int useGunType) {
        int isGetFog = wrap.getLongTermVal(FCRBattleOrderConstant.S_Fog);
        int gunrate = 100;
        switch (useGunType) {
            case FCRBattleOrderConstant.BattleSelectMenuTankFuGun:
                gunrate = itemLib.getItemOfID(gun2ID).getHitRate();
                break;
            case FCRBattleOrderConstant.BattleSelectMenuTankMainGun:
            case FCRBattleOrderConstant.BattleSelectMenuTankMainGunP:
                gunrate = itemLib.getItemOfID(gunID).getHitRate();
                break;
            case FCRBattleOrderConstant.BattleSelectMenuTankSE:
                gunrate = itemLib.getItemOfID(SEID).getHitRate();
                break;
        }
        double hitRate = (double) gunrate / 100 * 0.6 + driverSpeed * 0.001;
        //雷达的
        FCItem item = itemLib.getItemOfID(getRadarID());
        if (item != null && item.getCurrentDefense() != 0) {
            hitRate += ((double) Math.min(100, item.getHitRate()) / 100 * 0.15);
        }
        //C装置的
        item = itemLib.getItemOfID(getCdeviceID());
        if (item != null && item.getCurrentDefense() != 0) {
            hitRate += ((double) Math.min(100, item.getHitRate()) / 100 * 0.5);
        }
        if (isGetFog != -1) {//是否中烟雾
            hitRate = hitRate / 2;
        }
        return (int) (hitRate * 100);
    }

    //获取闪避率
    public int getDogeRate(int driverSpeed) {
        double doges = driverSpeed * 0.0005;
        //雷达的
        FCItem item = itemLib.getItemOfID(getRadarID());
        if (item != null && item.getCurrentDefense() != 0) {
            doges += ((double) Math.min(50, item.getDodgeRateAdd()) / 100 * 0.15);
        }
        //C装置的
        item = itemLib.getItemOfID(getCdeviceID());
        if (item != null && item.getCurrentDefense() != 0) {
            doges += ((double) Math.min(50, item.getDodgeRateAdd()) / 100 * 0.15);
        }
        return (int) (doges * 100);
    }

    //TODO 获取暴击值
    public int getCritRate(FCPlayerBattleWrap wrap) {
        int cirtRate = wrap.getLongTermVal(FCRBattleOrderConstant.S_AddCrit);
        cirtRate = cirtRate == -1 ? 0 : cirtRate;
        //雷达的
        FCItem item = itemLib.getItemOfID(getRadarID());
        if (item != null && item.getCurrentDefense() != 0) {
            cirtRate += item.getCrit();
        }
        //C装置的
        item = itemLib.getItemOfID(getCdeviceID());
        if (item != null && item.getCurrentDefense() != 0) {
            cirtRate += item.getCrit();
        }
        cirtRate = Math.max(0, cirtRate);
        cirtRate = Math.min(100, cirtRate);
        return cirtRate;
    }

    //是否开了气扇，要判断背包中的是否有具体道具并且是开的状态
    public boolean isOpenQiShan() {
        boolean ret = false;
        for (String k : knapsack) {
            if (k != null && k.contains("qiShan")) {
                FCItem item = itemLib.getItemOfID(k);
                if (item.isOnOFFState()) {
                    ret = true;
                    break;
                }
            }
        }
        return ret;
    }

    //是否开了空调
    public boolean isOpenKongTiao() {
        boolean ret = false;
        for (String k : knapsack) {
            if (k != null && k.contains("kongTiaoJi")) {
                FCItem item = itemLib.getItemOfID(k);
                if (item.isOnOFFState()) {
                    ret = true;
                    break;
                }
            }
        }
        return ret;
    }

    //是否开了暖风机
    public boolean isOpenNuanFengJi() {
        boolean ret = false;
        for (String k : knapsack) {
            if (k != null && k.contains("nuanFengJi")) {
                FCItem item = itemLib.getItemOfID(k);
                if (item.isOnOFFState()) {
                    ret = true;
                    break;
                }
            }
        }
        return ret;
    }

    //是否开了冷风机
    public boolean isOpenLengFengJi() {
        boolean ret = false;
        for (String k : knapsack) {
            if (k != null && k.contains("lengFengJi")) {
                FCItem item = itemLib.getItemOfID(k);
                if (item.isOnOFFState()) {
                    ret = true;
                    break;
                }
            }
        }
        return ret;
    }

    //是否有开迷彩条
    public boolean isOpenMiCaiTiao() {
        boolean ret = false;
        for (String k : knapsack) {
            if (k != null && k.contains("miCaiTiao")) {
                FCItem item = itemLib.getItemOfID(k);
                if (item.isOnOFFState()) {
                    ret = true;
                    break;
                }
            }
        }
        return ret;
    }

    //是否有开电子护屏
    public boolean isOpenHuPing() {
        boolean ret = false;
        for (String k : knapsack) {
            if (k != null && k.contains("dianBaoHuPing")) {
                FCItem item = itemLib.getItemOfID(k);
                if (item.isOnOFFState()) {
                    ret = true;
                    break;
                }
            }
        }
        return ret;
    }
}
